#include "MyLoger.h"
#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/Priority.hh>

using std::cout;
using std::endl;
using namespace log4cpp;

//ctrl + v(可视化)   l(右移光标)  j(下移光标)   
//x(删除可视化空白)
MyLogger *MyLogger::_pInstance = nullptr;

MyLogger *MyLogger::getInstnace()
{
    if(nullptr == _pInstance)
    {
        _pInstance = new MyLogger();
    }
    return _pInstance;
}

void MyLogger::destroy()
{
    if(_pInstance)
    {
        delete _pInstance;
        _pInstance = nullptr;
    }
}

//就是为了记录日志的
void MyLogger::warn(const char * msg)
{
    _root.warn(msg);
}

void MyLogger::error(const char * msg)
{
    _root.error(msg);
}

void MyLogger::debug(const char * msg)
{
    _root.debug(msg);
}

void MyLogger::info(const char * msg)
{
    _root.info(msg);
}

MyLogger::MyLogger()
: _root(log4cpp::Category::getRoot().getInstance("Root"))
{
    cout << "MyLogger()" << endl;
    //1、日志的格式
    PatternLayout *ppl1 = new PatternLayout();
    ppl1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout *ppl2 = new PatternLayout();
    ppl2->setConversionPattern("%d %c [%p] %m%n");

    //2、输出的目的地
    //一个是屏幕 一个是文件
    OstreamAppender *poa = 
        new OstreamAppender("OstreamAppender", &cout);
    poa->setLayout(ppl1);//设置格式

    FileAppender *pfa = 
        new FileAppender("FileAppender", "wd.txt");
    pfa->setLayout(ppl2);

    /* //3、创建的Category的对象(日志记录器) */
    _root.addAppender(poa);//添加目的地
    _root.addAppender(pfa);//添加目的地

    //4、设置优先级
    _root.setPriority(Priority::INFO);
}

MyLogger::~MyLogger()
{
    cout << "~MyLoger()" << endl;
    //回收空间
    Category::shutdown();
}

